package com.zhuannimei.utility;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;

/**
 * 文字处理工具类
 * 
 * @author Jack 2013-8-29
 * 
 * 
 */
public class TextHelper {

	private static final String TAG = "TextHelper";

	private static SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");// 24 小时制

	private static final int TIMESHOW = 3;

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		return SDF.format(date);
	}

	/**
	 * 
	 * @param time
	 * @return
	 */
	public static String getCurrentHours() {
		return SDF.format(new Date()).split(" ")[1].split(":")[0];
	}

	/**
	 * 截取HH:mm串 2013-09-06 14:20:00
	 * 
	 * @param time
	 * @return
	 */
	private static String getHHMMTime(String time) {
		String[] sts = time.split(" ");
		String[] strs = sts[1].split(":");
		return strs[0] + ":" + strs[1];
	}

	/**
	 * 
	 * @param time
	 * @return
	 */
	private static String getMMDDHHMMTime(String time) {
		String[] sts = time.split(" ");
		String[] str2s = sts[0].split("-");
		String[] strs = sts[1].split(":");
		return str2s[1] + "-" + str2s[2] + "  " + strs[0] + ":" + strs[1];
	}
	

	/**
	 * 简单比较今天和昨天的区分方法
	 * @param time
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	private static boolean isToday(String time, Date date) throws ParseException {
		String[] stsTime = time.split(" ");
		String[] str1 = stsTime[0].split("-");//动态的时间
		
		String nowTime = SDF.format(date);
		String[] stsNowTime = nowTime.split(" ");
		String[] srt2 = stsNowTime[0].split("-");//当前的时间
		if(str1[2].equals(srt2[2])){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 简化时间展现方式 2013-09-06 14:20:00 和当前系统时间做比较，细化到分钟就可以
	 * 
	 * @param time
	 * @return
	 */
	public static String shortTime(String time) {
		try {
			long times = SDF.parse(time).getTime();
			Date date = new Date();
			long timeNow = date.getTime();
			long diff = timeNow - times;
			// 天
			long days = diff / (1000 * 60 * 60 * 24);
			// 时
			long hours = (diff - days * (1000 * 60 * 60 * 24))
					/ (1000 * 60 * 60);
			// 分
			long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60))
					/ (1000 * 60);
			// xx分钟前
			if (days == 0 && hours == 0) {
				if (minutes <= 1) {
					return "1分钟前";
				} else {
					return minutes + "分钟前";
				}
			}
			boolean isToday = isToday(time, date);
			if (days==0 && hours!=0 && isToday) {
				// 显示 今天 HH：MM
				return "今天" + getHHMMTime(time);
			}else {
				// 显示 MM-DD HH：MM
				return getMMDDHHMMTime(time);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return time;
		}
	}

	/**
	 * 是否显示时间控件
	 * 
	 * @param time
	 * @return
	 */
	public static boolean isTimeShow(String firstTime, String nextTime) {
		// 显示时间 但是只显示一次
		try {
			long firstTimes = SDF.parse(firstTime).getTime();
			long nextTimes = SDF.parse(nextTime).getTime();
			long diff = nextTimes - firstTimes;
			// 天
			long days = diff / (1000 * 60 * 60 * 24);
			// 时
			long hours = (diff - days * (1000 * 60 * 60 * 24))
					/ (1000 * 60 * 60);
			// 分
			long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours
					* (1000 * 60 * 60))
					/ (1000 * 60);
			if (minutes < TIMESHOW && hours == 0 && days == 0) {
				return false;
			} else {
				return true;
			}
		} catch (ParseException e) {
			return true;
		}
	}

	/**
	 * 加亮 @****： 或 @****: 或@****[空格] <br>
	 * Html.fromHtml(String) *
	 * 
	 * @param str
	 * @return
	 */
	public static Spanned highLight(String str) {
		Pattern pattern = Pattern.compile("@[^\\s:：]+[:：\\s]");
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			String m = matcher.group();
			str = str.replace(m, "<font color=Navy>" + m + "</font>");
		}
		return Html.fromHtml(str);
	}

	/**
	 * 获取字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String[] getImageUrl(String str) {
		return str.split(",");
	}

	/**
	 * 加亮 @****： 或 @****: 或@****[空格] <br>
	 * SpannableString.setSpan(Object what, int start, int end, int flags)
	 * 
	 * @param text
	 * @return
	 */
	// 这个和hightLight(String str) 的效率对比?
	public static SpannableString light(CharSequence text) {
		SpannableString spannableString = new SpannableString(text);
		Pattern pattern = Pattern.compile("@[^\\s:：]+[:：\\s]");
		Matcher matcher = pattern.matcher(spannableString);
		while (matcher.find()) {
			spannableString.setSpan(new ForegroundColorSpan(Color.GREEN),
					matcher.start(), matcher.end(),
					Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return spannableString;
	}

	/**
	 * 格式化名字 <br>
	 * 用于保存微博图像，截取url的最后一段做为图像文件名
	 * 
	 * @param url
	 * @return
	 */
	public static String formatName(String url) {
		if (url == null || "".equals(url)) {
			return url;
		}
		int start = url.lastIndexOf("/");
		int end = url.lastIndexOf(".");
		if (start == -1 || end == -1) {
			return url;
		}
		return url.substring(start + 1, end);
	}

	/**
	 * 格式化来源
	 * 
	 * @param name
	 * @return
	 */
	public static String formatSource(String name) {
		if (name == null || "".equals(name)) {
			return name;
		}
		int start = name.indexOf(">");
		int end = name.lastIndexOf("<");
		if (start == -1 || end == -1) {
			return name;
		}
		return name.substring(start + 1, end);
	}



	/**
	 * 半角转全角
	 * 
	 * @param str
	 * @return
	 */
	public static CharSequence half2Full(String str) {

		char c[] = str.toCharArray();

		for (int i = 0; i < c.length; i++) {
			if (c[i] == ' ') {
				c[i] = '\u3000';

			} else if (c[i] < '\177') {
				c[i] = (char) (c[i] + 65248);
			}

		}
		return new String(c);
	}

	/**
	 * 截取一个字符串的首尾
	 * 
	 * @param str
	 * @return
	 */
	public static String TWSubString(String str) {
		return str.substring(1, str.length());

	}

	/**
	 * 替换内容中的特殊字符
	 * 
	 * @param content
	 * @return
	 */
	public static String replaceString(String content, Context context) {
		// 替换制表符 换行等
		content = content.trim();
		content = content.replaceAll("/n|/t|/r", "");
		content = content.toString().replaceAll("&gt;", ">");
		content = content.toString().replaceAll("&lt;", "<");
		// 替换表情
		content = content.replaceAll("&#091;", "[");

		content = content.replaceAll("&#093;", "]");

		content = content.replace("草你妈", "***");

		return content;
	}
}
